<template>
<div class="merchandise-management views-container">
    <div class="wlm-table">
         <div class="wlm-table-hearder-btn" >
             <el-button size="small" type="primary" @click="showdialog(1)">添加免单分类</el-button>
         </div>
         <!-- 添加对话框 -->
         <el-dialog @close="saveDialogForm" :visible.sync="dialogFormVisible" custom-class="reset-pwd wlm-form-dialog dialog-table">
            <div  class="wlm-form" v-if="dialogFormVisible">
              <div class="wlm-form-header wlm-dialog-fixheader">
              <span class="wlm-dialog-inlenr">添加免单分类</span>
            </div>
             <el-scrollbar wrap-class="scrollbar-wrapper">
        <div class="wlm-form-content">
          <div class="wlm-dialog-main">
            <div class="wlm-dialog-content">
              <el-form  :ref="formFormatData.key" :model="formFormatData.formData" :rules="formFormatData.rules" size="small" label-width="100px" class="retail-form" label-position="right">
              <el-form-item label="分类名字" prop="sort_name">
                <el-input  v-model.trim="formFormatData.formData.sort_name" :disabled="formFormatData.formData.default==1?true:false" placeholder="请输入商家分类名字" style="width:160px;" :maxlength="4">
                </el-input>
                 <div class="form-help">名字不能超过4个字</div>
              </el-form-item>
               <!-- <el-form-item label="分类图片" ref="merchantlog">
                  <choose-files ref="chooseFiles" :config="chooseLogoConfig" :changeEvt="formFormatFiltersChange"></choose-files>
                 <div class="form-help">建议尺寸：200*200像素，待定，需修改</div>
              </el-form-item> -->
              <el-form-item label="排序" prop="sort">
                <el-input onkeyup="this.value=this.value.replace(/[^\d.]/g,'');" maxlength="5" :controls="false" v-model.trim="formFormatData.formData.sort" :disabled="formFormatData.formData.default==1?true:false" placeholder="排序" style="width:160px;"></el-input>
                <div class="form-help">注:数值越大越靠前</div>
              </el-form-item>
              <el-form-item label="是否显示"  prop="is_status">
                <!-- <el-switch v-model="formFormatData.formData.is_status" active-value="1" inactive-value="2">
                    </el-switch> -->
                     <el-radio-group v-model.trim="formFormatData.formData.is_status">
                    <el-radio :label="1">开启</el-radio>
                    <el-radio :label="2">关闭</el-radio>
                  </el-radio-group>
              </el-form-item>
            </el-form>
            </div>
          </div>
        </div>
             </el-scrollbar>
            </div>
            <footer class="flex-row flex-align-c flex-justify-c wlm-dialog-fixfooter">
              <el-button size="small" @click="formFormatSubmit" type="primary">保存</el-button>
            </footer>
        </el-dialog>
         <!-- 列表数据 -->
        <div class="wlm-table-content">
        <el-table
          style="width:100%"
          :ref="tableFormatData.SortList.key"
          :data="tableFormatData.SortList.tableData"
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            prop="avatarUrl"
            label="ID"
          >
            <template slot-scope="scope">
                <span>{{scope.row.sort_id}}</span>
            </template>
          </el-table-column>
          <!-- <el-table-column
            prop="avatarUrl"
            label="图片"
          >
            <template slot-scope="scope">
                <img class="logo" v-if="scope.row.image" :src="scope.row.image.length>0?scope.row.image[0].file_path:''">
            </template>
          </el-table-column> -->
          <el-table-column
            prop=""
            label="名称"
          >
            <template slot-scope="scope">
                <span>{{scope.row.sort_name}}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop=""
            label="排序"
          >
            <template slot-scope="scope">
                <span>{{scope.row.sort}}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="date"
            label="创建时间"
          >
          <template slot-scope="scope">
              <span>{{scope.row.add_time| parseTime('{y}-{m}-{d} {h}:{i}:{s}')}}</span>
          </template>
          </el-table-column>
           <el-table-column prop="name" label="是否启用" >
              <template slot-scope="scope">
                <!-- sortShow -->
                <!--  -->
            <el-switch :active-value="1" :inactive-value="2" @change="changeTableItem({sort_id:scope.row.sort_id,is_status:scope.row.is_status},formFormatData.api.change.is_show)"  v-model="scope.row.is_status"> </el-switch>
          </template>
         </el-table-column>
          <el-table-column
            prop="date"
            label="操作"
          >
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="showdialog(2,scope.row.sort_id)">编辑</el-button>
            <el-button type="danger" size="mini"  @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
          </el-table-column>
        </el-table>
        <div class="pagination-content flex-row flex-justify-e flex-align-c">
          <el-pagination
            :disabled="!hasTableData"
            @size-change="listPageChange"
            @current-change="listPageChange"
            :current-page.sync="tableFormatData.SortList.pagination.page"
            :page-sizes="tableFormatData.SortList.pagination.pagesizes"
            :page-size.sync="tableFormatData.SortList.pagination.list_rows"
            layout="total, sizes, prev, pager, next, jumper"
            :total="hasTableData?tableFormatData.SortList.pagination.total : 0"
          >
          </el-pagination>
        </div>

      </div>
    </div>
</div>
</template>
<script>
import { editFreeSort, getFreeSortList, getOneFreeSort, delFreeSort,freeSortShow } from '@/api/merchant'
// import { getSortList, editSort, delSort, addSort, getOneSort, sortShow } from '@/api/merchant'
import mixins from '@/mixins/mixins'
import ChooseFiles from '@/components/ChooseFiles/index'
export default {
  mixins: [mixins.getters('Table'), mixins.getters('Form')],
  name: 'SortList',
  components: {
    ChooseFiles
  },
  data() {
    return {
      loading: false,
      pindex: '',
      timer: '',
      dialogFormVisible: false,
      formLabelWidth: '120px',
    //   chooseLogoConfig: {
    //     type: ['image'],
    //     model: 'display',
    //     uploadConfig: {
    //       limit: 1,
    //       model: 'display'
    //     },
    //     initList: [],
    //     // isShowList: true,
    //     file_type: 'image',
    //     bindKey: {
    //       key: 'image',
    //       model: 'img'
    //     }
    //   },
      formFormatData: {
        key: 'SortList',
        model: {
          noCreateEdit: false,
          bindDialog: 'dialogFormVisible'
        },
        api: {
          editForm: {// 编辑回填
            api: getOneFreeSort,
            params: {
              sort_id: this.sort_id
            },
            filters: {
              img: {
                bindInit: ['chooseLogoConfig']
              }
            },
            redirect: 'sort_id'
          },
          submitForm: editFreeSort,
          change: {
            is_show: {
              key: 'is_show',
              api: freeSortShow
            }
          }
        },
        formData: {
          sort_name: '',
          sort: '',
          is_status: 1,
          default: ''
        },
        rules: {
          sort_name: [{
            message: '请填写分类名称',
            required: true,
            trigger: 'change'
          }],
        //   image: [{
        //     message: '请上传图片',
        //     required: true,
        //     trigger: 'change'
        //   }],
          sort: [{
            message: '请填写排序',
            required: true,
            trigger: 'change'
          }],
          is_status: [{
            message: '请选择是否显示',
            required: true,
            trigger: 'change'
          }]
        }
      },
      tableFormatData: {
        current: 'SortList',
        SortList: {
          key: 'SortList',
          api: {
            getList: getFreeSortList
          },
          tableData: [],
          files: {
            name: '',
            ids: [],
            checkall: '0',
            live_state: 'all',
            sort_name: '',
            sort_id: ''
          },
          pagination: {
            page: 1,
            list_rows: 10,
            pagesizes: [10, 20, 50],
            total: 0
          }
        }
      }

    }
  },
  methods: {
    showdialog(type, params) {
            console.log("55555555555555555",type,params)

      if (type === 1) {
        // this.formFormatData=[]
        // this.formFormatData.api.submitForm = addSort
        this.dialogFormVisible = true
      }
      if (type === 2) {
        this.formFormatData.api.submitForm = editFreeSort
        this.formFormatDialogEditEvt({ id: params, toggle: 'dialogFormVisible' })
      }
    },

    handleDelete(index, row) { // 删除
      console.log(index, row)
      this.$confirm('此操作将删除此行数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // this.$message({
        //   type: 'success',
        //   message: '删除成功!'
        // });
        delFreeSort({ sort_id: row.sort_id }).then(res => {
          const { code } = res.data
          code == 1 && this.$message.success('操作成功')
        //   this.$router.go(0)
        this.filesSerch()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    saveDialogForm() {
      this.resetDialogFormData({ sort_name: '', sort: '', is_status: '' })
    }
  }
}
</script>
<style lang="scss" scoped>
        .form-help{
            color: #999;
            line-height: 14px;
            margin-top: 10px;
            font-size: 12px;
            font-weight: 400;
          }
</style>
